]> git.saurik.com Git - bison.git/blame - data/c++.m4
c: improve the definition of public types
[bison.git] / data / c++.m4
CommitLineData
08af01c2 1 -*- Autoconf -*-
cf147260
AD
2
3# C++ skeleton for Bison
4
34136e65 5# Copyright (C) 2002-2012 Free Software Foundation, Inc.
cf147260 6
f16b0819 7# This program is free software: you can redistribute it and/or modify
cf147260 8# it under the terms of the GNU General Public License as published by
f16b0819 9# the Free Software Foundation, either version 3 of the License, or
cf147260 10# (at your option) any later version.
f16b0819 11#
cf147260
AD
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
f16b0819 16#
cf147260 17# You should have received a copy of the GNU General Public License
f16b0819 18# along with this program. If not, see <http://www.gnu.org/licenses/>.
cf147260 19
3953ed88
AD
20m4_include(b4_pkgdatadir/[c.m4])
21
38de4e57
AD
22# b4_comment(TEXT, [PREFIX])
23# --------------------------
24# Put TEXT in comment. Prefix all the output lines with PREFIX.
25m4_define([b4_comment],
26[b4_comment_([$1], [$2// ], [$2// ])])
27
28
cf147260
AD
29## ---------------- ##
30## Default values. ##
31## ---------------- ##
32
a4e25e1d 33b4_percent_define_default([[parser_class_name]], [[parser]])
7789b6e3
AD
34
35# Don't do that so that we remember whether we're using a user
36# request, or the default value.
37#
db8ab2be 38# b4_percent_define_default([[api.location.type]], [[location]])
7789b6e3 39
a4e25e1d 40b4_percent_define_default([[filename_type]], [[std::string]])
171ad99d 41b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix]))
67501061 42
c1d19e10 43b4_percent_define_default([[global_tokens_and_yystype]], [[false]])
a4e25e1d
JD
44b4_percent_define_default([[define_location_comparison]],
45 [m4_if(b4_percent_define_get([[filename_type]]),
c1d19e10 46 [std::string], [[true]], [[false]])])
cf147260
AD
47
48
67501061 49
793fbca5
JD
50## ----------- ##
51## Namespace. ##
52## ----------- ##
53
67501061
AD
54m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
55
793fbca5
JD
56
57# Don't permit an empty b4_namespace_ref. Any `::parser::foo' appended to it
58# would compile as an absolute reference with `parser' in the global namespace.
59# b4_namespace_open would open an anonymous namespace and thus establish
60# internal linkage. This would compile. However, it's cryptic, and internal
61# linkage for the parser would be specified in all translation units that
62# include the header, which is always generated. If we ever need to permit
63# internal linkage somehow, surely we can find a cleaner approach.
64m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
67501061 65[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
66 [[namespace reference is empty]])])
67
68# Instead of assuming the C++ compiler will do it, Bison should reject any
69# invalid b4_namepsace_ref that would be converted to a valid
70# b4_namespace_open. The problem is that Bison doesn't always output
71# b4_namespace_ref to uncommented code but should reserve the ability to do so
72# in future releases without risking breaking any existing user grammars.
73# Specifically, don't allow empty names as b4_namespace_open would just convert
74# those into anonymous namespaces, and that might tempt some users.
75m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
67501061 76[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
77 [[namespace reference has consecutive "::"]])])
78m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
67501061 79[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
80 [[namespace reference has a trailing "::"]])])
81
82m4_define([b4_namespace_open],
67501061 83[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
793fbca5
JD
84[namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
85 [^\(.\)[ ]*::], [\1])),
86 [::], [ { namespace ])[ {]])])
87
88m4_define([b4_namespace_close],
67501061 89[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
4977e0a7 90m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
b987342b 91 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
793fbca5 92 [\1])),
b987342b 93 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
793fbca5
JD
94
95
e3990e3c
AD
96# b4_token_enums
97# --------------
c095d689 98# Output the definition of the tokens as enums.
cf147260 99m4_define([b4_token_enums],
e3990e3c
AD
100[[enum yytokentype
101 {
102 ]m4_join([,
103 ],
104 b4_symbol_map([b4_token_enum]))[
105 };]dnl
c095d689 106])
cf147260
AD
107
108
ef51bfa7
AD
109
110
cf147260
AD
111## ----------------- ##
112## Semantic Values. ##
113## ----------------- ##
114
b9e4eb5b
AD
115# b4_semantic_type_declare
116# ------------------------
117# Declare semantic_type.
118m4_define([b4_semantic_type_declare],
119[ /// Symbol semantic values.
120m4_ifdef([b4_stype],
121[ union semantic_type
122 {b4_user_stype
123 };],
124[m4_if(b4_tag_seen_flag, 0,
125[[ typedef int semantic_type;]],
5458913a 126[[ typedef ]b4_api_PREFIX[STYPE semantic_type;]])])])
b9e4eb5b
AD
127
128
4f84717d
AD
129# b4_public_types_declare
130# -----------------------
131# Define the public types: token, semantic value, location, and so forth.
1d6b689b 132# Depending on %define token_lex, may be output in the header or source file.
4f84717d 133m4_define([b4_public_types_declare],
5458913a 134[[#ifndef ]b4_api_PREFIX[STYPE
4f84717d
AD
135]b4_semantic_type_declare[
136#else
5458913a 137 typedef ]b4_api_PREFIX[STYPE semantic_type;
4f84717d
AD
138#endif]b4_locations_if([
139 /// Symbol locations.
f6b561d9 140 typedef b4_percent_define_get([[api.location.type]],
7789b6e3 141 [[location]]) location_type;])[
1d6b689b 142
ff601366
AD
143 /// Syntax errors thrown from user actions.
144 struct syntax_error : std::runtime_error
145 {
146 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([
147 location_type location;])[
148 };
149
4f84717d
AD
150 /// Tokens.
151 struct token
152 {
e3990e3c 153 ]b4_token_enums[
4f84717d 154 };
1d6b689b 155
4f84717d
AD
156 /// Token type.
157 typedef token::yytokentype token_type;
1d6b689b
AD
158
159 /// A complete symbol, with its type.
160 template <typename Exact>
161 struct symbol_base_type
162 {
163 /// Default constructor.
164 inline symbol_base_type ();
165
166 /// Constructor.]b4_locations_if([
ee52f6ec 167 inline symbol_base_type (const location_type& l);])[
710c4a65 168 inline symbol_base_type (]b4_join(
1d6b689b
AD
169 [const semantic_type& v],
170 b4_locations_if([const location_type& l]))[);
171
172 /// Return this with its exact type.
173 const Exact& self () const;
174 Exact& self ();
175
176 /// Return the type of this symbol.
177 int type_get () const;
178
179 /// The semantic value.
180 semantic_type value;]b4_locations_if([
181
182 /// The location.
183 location_type location;])[
184 };
185
186 /// External form of a symbol: its type and attributes.
187 struct symbol_type : symbol_base_type<symbol_type>
188 {
189 /// The parent class.
190 typedef symbol_base_type<symbol_type> super_type;
191
192 /// Default constructor.
193 inline symbol_type ();
194
7be08dfb
AD
195 /// Destructive move, \a s is emptied.
196 inline void move (symbol_type& s);
197
2b08bceb 198 /// Constructor for tokens with semantic value.
710c4a65 199 inline symbol_type (]b4_join([token_type t],
1d6b689b
AD
200 [const semantic_type& v],
201 b4_locations_if([const location_type& l]))[);
202
2b08bceb 203 /// Constructor for valueless tokens.
710c4a65 204 inline symbol_type (]b4_join([token_type t],
1d6b689b
AD
205 b4_locations_if([const location_type& l]))[);
206
207 /// The symbol type.
208 int type;
209
2b08bceb 210 /// The symbol type.
1d6b689b
AD
211 inline int type_get_ () const;
212
2b08bceb 213 /// The token.
1d6b689b
AD
214 inline token_type token () const;
215 };
0623bacc 216]b4_symbol_constructor_declare])
4f84717d
AD
217
218
1d6b689b
AD
219# b4_public_types_define
220# ----------------------
221# Provide the implementation needed by the public types.
222m4_define([b4_public_types_define],
ff601366
AD
223[[ inline
224 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
225 : std::runtime_error (m)]b4_locations_if([
226 , location (l)])[
227 {}
228
229 // symbol_base_type.
1d6b689b 230 template <typename Exact>
ff601366 231 inline
1d6b689b
AD
232 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type ()
233 : value()]b4_locations_if([
234 , location()])[
235 {
236 }]b4_locations_if([[
237
238 template <typename Exact>
29660062 239 inline
1d6b689b
AD
240 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (const location_type& l)
241 : value()
242 , location(l)
243 {
244 }]])[
245
246 template <typename Exact>
29660062 247 inline
710c4a65 248 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (]b4_join(
1d6b689b
AD
249 [const semantic_type& v],
250 b4_locations_if([const location_type& l]))[)
251 : value(v)]b4_locations_if([
252 , location(l)])[
253 {
254 }
255
256 template <typename Exact>
29660062 257 inline
1d6b689b
AD
258 const Exact&
259 ]b4_parser_class_name[::symbol_base_type<Exact>::self () const
260 {
261 return static_cast<const Exact&>(*this);
262 }
263
264 template <typename Exact>
29660062 265 inline
1d6b689b
AD
266 Exact&
267 ]b4_parser_class_name[::symbol_base_type<Exact>::self ()
268 {
269 return static_cast<Exact&>(*this);
270 }
271
272 template <typename Exact>
29660062 273 inline
1d6b689b
AD
274 int
275 ]b4_parser_class_name[::symbol_base_type<Exact>::type_get () const
276 {
277 return self ().type_get_ ();
278 }
279
280 // symbol_type.
29660062 281 inline
1d6b689b
AD
282 ]b4_parser_class_name[::symbol_type::symbol_type ()
283 : super_type ()
284 , type ()
285 {
286 }
287
29660062 288 inline
710c4a65 289 ]b4_parser_class_name[::symbol_type::symbol_type (]b4_join(
2b08bceb 290 [token_type t],
1d6b689b
AD
291 b4_locations_if([const location_type& l]))[)
292 : super_type (]b4_locations_if([l])[)
2b08bceb 293 , type (yytranslate_ (t))
1d6b689b
AD
294 {
295 }
296
29660062 297 inline
710c4a65 298 ]b4_parser_class_name[::symbol_type::symbol_type (]b4_join(
2b08bceb 299 [token_type t],
1d6b689b
AD
300 [const semantic_type& v],
301 b4_locations_if([const location_type& l]))[)
302 : super_type (v]b4_locations_if([, l])[)
2b08bceb 303 , type (yytranslate_ (t))
1d6b689b
AD
304 {
305 }
306
7be08dfb
AD
307 inline
308 void
309 ]b4_parser_class_name[::symbol_type::move (symbol_type& s)
310 {
311 ]b4_variant_if([b4_symbol_variant([[s.type]], [value], [build], [s.value])],
312 [value = s.value;])[
313 type = s.type;]b4_locations_if([
314 location = s.location;])[
315 }
316
29660062 317 inline
1d6b689b
AD
318 int
319 ]b4_parser_class_name[::symbol_type::type_get_ () const
320 {
321 return type;
322 }
e36ec1f4 323]b4_token_ctor_if([[
29660062 324 inline
1d6b689b
AD
325 ]b4_parser_class_name[::token_type
326 ]b4_parser_class_name[::symbol_type::token () const
327 {
328 // YYTOKNUM[NUM] -- (External) token number corresponding to the
329 // (internal) symbol number NUM (which must be that of a token). */
330 static
331 const ]b4_int_type_for([b4_toknum])[
332 yytoken_number_[] =
333 {
334 ]b4_toknum[
335 };
336 return static_cast<token_type> (yytoken_number_[type]);
337 }
0623bacc
AD
338]])[]dnl
339b4_symbol_constructor_define])
340
341
342# b4_symbol_constructor_declare
343# b4_symbol_constructor_define
344# -----------------------------
345# Declare/define symbol constructors for all the value types.
346# Use at class-level. Redefined in variant.hh.
347m4_define([b4_symbol_constructor_declare], [])
348m4_define([b4_symbol_constructor_define], [])
349
1d6b689b 350
cbf25ce7
AD
351# b4_yytranslate_define
352# ---------------------
353# Define yytranslate_. Sometimes used in the header file,
354# sometimes in the cc file.
355m4_define([b4_yytranslate_define],
356[[ // Symbol number corresponding to token number t.
357 ]b4_parser_class_name[::token_number_type
e36ec1f4 358 ]b4_parser_class_name[::yytranslate_ (]b4_token_ctor_if([token_type],
cbf25ce7
AD
359 [int])[ t)
360 {
361 static
362 const token_number_type
363 translate_table[] =
364 {
365]b4_translate[
366 };
367 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
368 const token_number_type undef_token_ = ]b4_undef_token_number[;
369
370 if (static_cast<int>(t) <= yyeof_)
371 return yyeof_;
372 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
373 return translate_table[t];
374 else
375 return undef_token_;
376 }
377]])
378
1d6b689b 379
cf147260
AD
380# b4_lhs_value([TYPE])
381# --------------------
382# Expansion of $<TYPE>$.
383m4_define([b4_lhs_value],
1fa5d8bb 384[b4_symbol_value([yyval], [$1])])
cf147260
AD
385
386
387# b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
388# --------------------------------------
389# Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
390# symbols on RHS.
391m4_define([b4_rhs_value],
1fa5d8bb
AD
392[b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
393
cf147260
AD
394
395# b4_lhs_location()
396# -----------------
397# Expansion of @$.
398m4_define([b4_lhs_location],
399[(yyloc)])
400
401
402# b4_rhs_location(RULE-LENGTH, NUM)
403# ---------------------------------
404# Expansion of @NUM, where the current rule has RULE-LENGTH symbols
405# on RHS.
406m4_define([b4_rhs_location],
d1ff7a7c 407[(yylocation_stack_@{($1) - ($2)@})])
cf147260
AD
408
409
410# b4_parse_param_decl
411# -------------------
412# Extra formal arguments of the constructor.
413# Change the parameter names from "foo" into "foo_yyarg", so that
414# there is no collision bw the user chosen attribute name, and the
415# argument name in the constructor.
416m4_define([b4_parse_param_decl],
417[m4_ifset([b4_parse_param],
418 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
419
420m4_define([b4_parse_param_decl_1],
421[$1_yyarg])
422
423
424
425# b4_parse_param_cons
426# -------------------
427# Extra initialisations of the constructor.
428m4_define([b4_parse_param_cons],
429 [m4_ifset([b4_parse_param],
e9690142 430 [
cf147260
AD
431 b4_cc_constructor_calls(b4_parse_param)])])
432m4_define([b4_cc_constructor_calls],
e9690142 433 [m4_map_sep([b4_cc_constructor_call], [,
cf147260
AD
434 ], [$@])])
435m4_define([b4_cc_constructor_call],
e9690142 436 [$2 ($2_yyarg)])
cf147260
AD
437
438# b4_parse_param_vars
439# -------------------
440# Extra instance variables.
441m4_define([b4_parse_param_vars],
442 [m4_ifset([b4_parse_param],
e9690142 443 [
ac826bc4 444 // User arguments.
cf147260
AD
445b4_cc_var_decls(b4_parse_param)])])
446m4_define([b4_cc_var_decls],
e9690142 447 [m4_map_sep([b4_cc_var_decl], [
cf147260
AD
448], [$@])])
449m4_define([b4_cc_var_decl],
e9690142 450 [ $1;])
426903aa
AD
451
452
453## ---------##
454## Values. ##
455## ---------##
456
457# b4_yylloc_default_define
458# ------------------------
459# Define YYLLOC_DEFAULT.
460m4_define([b4_yylloc_default_define],
461[[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
462 If N is 0, then set CURRENT to the empty location which ends
463 the previous symbol: RHS[0] (always defined). */
464
465# ifndef YYLLOC_DEFAULT
466# define YYLLOC_DEFAULT(Current, Rhs, N) \
467 do \
468 if (N) \
469 { \
470 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
471 (Current).end = YYRHSLOC (Rhs, N).end; \
472 } \
473 else \
474 { \
475 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
476 } \
477 while (/*CONSTCOND*/ false)
478# endif
479]])